package com.ulook.lookplanet.controller;

import com.ulook.lookplanet.bean.TCornUser;
import com.ulook.lookplanet.bean.TUser;
import com.ulook.lookplanet.bean.VUserInfo;
import com.ulook.lookplanet.common.ExceptionCode;
import com.ulook.lookplanet.common.JsonResult;
import com.ulook.lookplanet.service.HomeService;
import com.ulook.lookplanet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/3/27
 */
@RestController
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 获取用户页面展示的信息
     */
    @PostMapping("/getUserInfo")
    public JsonResult<VUserInfo> getUserInfo(HttpServletRequest request){
        String id = (request.getParameter("id"));
        if(id == null || "".equals(id)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        VUserInfo userInfo = homeService.getUserInfo(Integer.parseInt(id));
        System.out.println(userInfo.toString());
        return new JsonResult<>(userInfo);
    }

    /**
     * 获取基本信息
     */
    @PostMapping("/getTUserById")
    public JsonResult<TUser> getTUserById(HttpServletRequest request){
        String id = (request.getParameter("id"));
        if(id == null || "".equals(id)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        TUser user = homeService.getTUserById(Integer.parseInt(id));
        return new JsonResult<>(user);
    }

    /**
     * 是否关注
     */
    @PostMapping("/isCornUser")
    public JsonResult<TCornUser> isCornUser(HttpServletRequest request){
        String userId = request.getParameter("userId");
        String cornUserId = request.getParameter("cornUserId");
        if(StringUtil.isEmpty(userId) || StringUtil.isEmpty(cornUserId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        TCornUser cornUserParam = new TCornUser();
        cornUserParam.setUserid(Integer.parseInt(userId));
        cornUserParam.setCornuserid(Integer.parseInt(cornUserId));

        TCornUser cornUser = homeService.isCornUser(cornUserParam);
        return new JsonResult<>(cornUser);
    }

    /**
     * 点击关注
     */
    @PostMapping("/beCornUser")
    public JsonResult beCornUser(HttpServletRequest request){
        String userId = request.getParameter("userId");
        String cornUserId = request.getParameter("cornUserId");
        if(StringUtil.isEmpty(userId) || StringUtil.isEmpty(cornUserId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        TCornUser cornUserParam = new TCornUser();
        cornUserParam.setUserid(Integer.parseInt(userId));
        cornUserParam.setCornuserid(Integer.parseInt(cornUserId));
        homeService.beCornUser(cornUserParam);
        return new JsonResult();
    }

    /**
     * 获取关注列表
     */
    @PostMapping("/getFriends")
    public JsonResult<List<TUser>> getFriends(HttpServletRequest request){
        String id = (request.getParameter("id"));
        if(id == null || "".equals(id)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        List<TUser> users = homeService.getFriends(Integer.parseInt(id));
        return new JsonResult<>(users);
    }

    /**
     * 修改信息
     */
    @PostMapping("/updateUserInfo")
    public JsonResult updateUserInfo(HttpServletRequest request){
        String id = (request.getParameter("id"));
        if(StringUtil.isEmpty(id)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String gender = request.getParameter("gender");
        String star = request.getParameter("star");
        String city = request.getParameter("city");
        String hobby = request.getParameter("hobby");
        String sign = request.getParameter("sign");

        TUser user = new TUser();
        user.setId(Integer.parseInt(id));
        user.setName(name);
        user.setAge(StringUtil.isEmpty(age) ? 0 : Integer.parseInt(age));
        user.setGender(gender);
        user.setStarsign(star);
        user.setCity(city);
        user.setHobby(hobby);
        user.setSignature(sign);

        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("files");
        homeService.updateUserInfo(user, files);

        return new JsonResult();
    }

}
